home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 111_01 / utr.c < prev    next >
Text File  |  1985-08-19  |  2KB  |  100 lines

  1. /*
  2. HEADER:        ;
  3. TITLE:        Squeezer;
  4. DESCRIPTION:    "Auxiliary file for the SQ.C and USQ.C package.
  5.         See SQUEEZER.DOC.";
  6. SYSTEM:        CP/M-80;
  7. FILENAME:    UTR.C;
  8. SEE-ALSO:    SQUEEZER.DOC;
  9. AUTHORS:    Dick Greenlaw;
  10. COMPILERS:    BDS C;
  11. */
  12. #include <bdscio.h>
  13. #include <dio.h>
  14. #include "sqcom.h"
  15. #include "usq.h"
  16.  
  17. /* initialize decoding functions */
  18.  
  19. init_cr()
  20. {
  21.     repct = 0;
  22. }
  23.  
  24. init_huff()
  25. {
  26.     bpos = 99;    /* force initial read */
  27. }
  28.  
  29. /* Get bytes with decoding - this decodes repetition,
  30.  * calls getuhuff to decode file stream into byte
  31.  * level code with only repetition encoding.
  32.  *
  33.  * The code is simple passing through of bytes except
  34.  * that DLE is encoded as DLE-zero and other values
  35.  * repeated more than twice are encoded as value-DLE-count.
  36.  */
  37.  
  38. int
  39. getcr(ib)
  40. struct _buf *ib;
  41. {
  42.     int c;
  43.  
  44.     if(repct > 0) {
  45.         /* Expanding a repeated char */
  46.         --repct;
  47.         return value;
  48.     } else {
  49.         /* Nothing unusual */
  50.         if((c = getuhuff(ib)) != DLE) {
  51.             /* It's not the special delimiter */
  52.             value = c;
  53.             if(value == EOF)
  54.                 repct = LARGE;
  55.             return value;
  56.         } else {
  57.             /* Special token */
  58.             if((repct = getuhuff(ib)) == 0)
  59.                 /* DLE, zero represents DLE */
  60.                 return DLE;
  61.             else {
  62.                 /* Begin expanding repetition */
  63.                 repct -= 2;    /* 2nd time */
  64.                 return value;
  65.             }
  66.         }
  67.     }
  68. }
  69.  
  70. /* Decode file stream into a byte level code with only
  71.  * repetition encoding remaining.
  72.  */
  73.  
  74. int
  75. getuhuff(ib)
  76. struct _buf *ib;
  77. {
  78.     int i;
  79.     int bitval;
  80.  
  81.     /* Follow bit stream in tree to a leaf*/
  82.     i = 0;    /* Start at root of tree */
  83.     do {
  84.         if(++bpos > 7) {
  85.             if((curin = getc(ib)) == ERROR)
  86.                 return ERROR;
  87.             bpos = 0;
  88.             /* move a level deeper in tree */
  89.             i = dnode[i].children[1 & curin];
  90.         } else
  91.             i = dnode[i].children[1 & (curin >>= 1)];
  92.     } while(i >= 0);
  93.  
  94.     /* Decode fake node index to original data value */
  95.     i = -(i + 1);
  96.     /* Decode special endfile token to normal EOF */
  97.     i = (i == SPEOF) ? EOF : i;
  98.     return i;
  99. }
  100.